//
//  DYZCarSourceDetailController.m
//  DYZCars
//
//  Created by jdc on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZCarSourceDetailController.h"
#import "DYZCarDetailCell.h"
#import "DYZCarContentCell.h"
#import "DYZSellerCell.h"
#import "ShareCustom.h"
static NSString *headerCellId = @"headerCellId";
static NSString *detailCellId = @"detailCellId";
static NSString *connectCellId = @"connectCellId";

@interface DYZCarSourceDetailController ()<DYZSellerCellDelegate,UIAlertViewDelegate>
@property (nonatomic,assign) NSUInteger flag;
@property (nonatomic,weak) UIView *btnsView;
@end

@implementation DYZCarSourceDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupShare];
}
-(void)setupUI{
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZCarDetailCell" bundle:nil] forCellReuseIdentifier:headerCellId];
    [self.tableView registerClass:[DYZCarContentCell class] forCellReuseIdentifier:detailCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZSellerCell" bundle:nil] forCellReuseIdentifier:connectCellId];
    //设置自动行高
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
}
#pragma mark -UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 6;
    }else if(section==1){
        return 1;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            self.tableView.rowHeight = 80;
            DYZCarDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:headerCellId forIndexPath:indexPath];
            cell.carsModel = self.carsModel;
            return cell;
        }else{
            tableView.rowHeight = 50;
            DYZCarContentCell *cell = [tableView dequeueReusableCellWithIdentifier:detailCellId forIndexPath:indexPath];
            if (indexPath.row==1) {
                cell.keyLable.text = @"车源类型";
                cell.valueLable.text = self.carsModel.carType;
            }
            if (indexPath.row==2) {
                cell.keyLable.text = @"成交价";
                cell.valueLable.text =[NSString stringWithFormat:@"%zd",self.carsModel.dealPrice];
            }
            if (indexPath.row==3) {
                cell.keyLable.text = @"颜色";
                cell.valueLable.text =[NSString stringWithFormat:@"%@/%@",self.carsModel.outsideColor,self.carsModel.insideColor];
            }
            
            if (indexPath.row==4) {
                cell.keyLable.text = @"销售区域";
                cell.valueLable.text = self.carsModel.city;
            }
            if (indexPath.row==5) {
                cell.keyLable.text = @"备注";
                cell.valueLable.text =[NSString stringWithFormat:@"加配后的指导价为:%zd元\n有意者，请尽快联系",self.carsModel.guidePrice];
            }
            return cell;
        }
    }else{
        DYZSellerCell *cell = [tableView dequeueReusableCellWithIdentifier:connectCellId forIndexPath:indexPath];
        tableView.rowHeight = 50;
        cell.carsModel = self.carsModel;
        cell.delegate = self;
        return cell;
    }
}
#pragma mark -UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //获取indexPath
    NSIndexPath *indexPath = [[NSIndexPath alloc]initWithIndex:section];
    if (indexPath.section == 0) {//第一组
        return 0;
        
    }else if(indexPath.section ==1){
        return 30;
    }else
        return 10;
    
}
/**
 *  组头
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    UILabel *lable = [[UILabel alloc]init];
    [view addSubview:lable];
    lable.text = @"联系人";
    lable.font = [UIFont systemFontOfSize:12];
    [lable sizeToFit];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.equalTo(view);
    }];
    view.backgroundColor = [UIColor lightGrayColor];
    if (section==1) {
        return view;
        
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark- DYZSellerCellDelegate
-(void)sellerCell:(DYZSellerCell *)DYZSellerCell{
    NSString *title=@"拨打电话";
    NSString *numberButtonTitle1=[NSString stringWithFormat:@"联系电话1\t%@",DYZSellerCell.sellerPhoneNumber.text];
    NSString *numberButtonTitle2=@"联系电话2\t13538237222";
    NSString *cancelButtonTitle=@"取消";
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle: title message:nil preferredStyle:0];//创建界面
    UIWebView *callWebview = [[UIWebView alloc] initWithFrame:CGRectMake(265, 425, 120, 150)];
    callWebview.backgroundColor = [UIColor orangeColor];
    UIAlertAction *numberAction1=[UIAlertAction actionWithTitle:numberButtonTitle1 style:UIAlertActionStyleDestructive handler:^(UIAlertAction *acton){
            NSString *numStr = DYZSellerCell.sellerPhoneNumber.text;
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",numStr];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view.window addSubview:callWebview];
        }];//创建按钮numberAction1以及对应事件
    UIAlertAction *numberAction2=[UIAlertAction actionWithTitle:numberButtonTitle2 style:0 handler:^(UIAlertAction *acton){
        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"13538237222"];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view.window addSubview:callWebview];
}];//创建按钮numberAction2以及对应事件
    
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:cancelButtonTitle style:0 handler:^(UIAlertAction *acton){}];//创建按钮cancel以及对应事件
    [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];//设置取消按钮颜色
    
    //最后将这些按钮都添加到界面上去，显示界面
    [alertController addAction:numberAction1];
    [alertController addAction:numberAction2];
    [alertController addAction:cancelAction];
    //
    
    [self presentViewController: alertController animated:YES completion:nil];
}

#pragma mark- 添加分享按钮
-(void)setupShare{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"u832"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:btn];
//    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"u832"] style:UIBarButtonItemStylePlain target:self action:@selector(jump)];
     self.navigationItem.rightBarButtonItem = rightBar;
    
}
-(void)jump{
    
    if (!self.flag) {
        UIView * view = [[UIView alloc]init];
        view.backgroundColor = [UIColor blueColor];
        [self.view.window addSubview:view];
        self.btnsView = view;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.top.equalTo(self.navigationController.navigationBar.mas_bottom);
            make.right.equalTo(self.view.window);
            make.width.offset(80);
            make.height.offset(180);
        }];
        UIButton *oneBtn = [[UIButton alloc]init];
        UIButton *twoBtn = [[UIButton alloc]init];
        UIButton *threeBtn = [[UIButton alloc]init];
        UIButton *fourBtn = [[UIButton alloc]init];
//        [oneBtn setBackgroundColor:[UIColor blueColor]];
//        [twoBtn setBackgroundColor:[UIColor purpleColor]];
//        [threeBtn setBackgroundColor:[UIColor redColor]];
//        [fourBtn setBackgroundColor:[UIColor orangeColor]];
        [oneBtn setTitle:@"收藏" forState:UIControlStateNormal];
        [twoBtn setTitle:@"分享" forState:UIControlStateNormal];
        [threeBtn setTitle:@"收定金" forState:UIControlStateNormal];
        [fourBtn setTitle:@"成交" forState:UIControlStateNormal];
        //    oneBtn.frame = CGRectMake(0, 0, 80, <#CGFloat height#>)
        [view addSubview:oneBtn];
        [view addSubview:twoBtn];
        [view addSubview:threeBtn];
        [view addSubview:fourBtn];
        NSArray *btnArr = @[oneBtn,twoBtn,threeBtn,fourBtn];
        for (int i = 0 ; i < btnArr.count; i++) {
            UIButton *btn = btnArr[i];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        [oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(view);
            make.height.offset(45);
        }];
        [twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(oneBtn.mas_bottom);
            make.left.right.equalTo(view);
            make.height.equalTo(oneBtn);
        }];
        [threeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(twoBtn.mas_bottom);
            make.left.right.equalTo(view);
            make.height.equalTo(twoBtn);
        }];
        [fourBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(threeBtn.mas_bottom);
            make.left.right.equalTo(view);
            make.height.equalTo(threeBtn);
        }];
        //分享按钮的事件
        [twoBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
        //收定金事件
        [threeBtn addTarget:self action:@selector(collectDeposit:) forControlEvents:UIControlEventTouchUpInside];
        self.flag = 1;
        
    }else{
        //        [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.btnsView removeFromSuperview];
        self.flag = 0;
    }
}
-(void)shareClick:(UIButton *)sender{
    [ShareCustom shareWithContent:nil];
   
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.btnsView removeFromSuperview];

}
//点击收定金事件
-(void)collectDeposit:(UIButton *)sender{
    NSString *message = [NSString stringWithFormat:@"确定收取定金后，当前车源信息将变更为“已收定金”状态，请在“我的车源”中继续其他操作其他状态"];
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle: nil message:message preferredStyle:UIAlertControllerStyleAlert];//创建界面
    UIAlertAction *fixAction=[UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction *acton){}];//创建按钮numberAction2以及对应事件
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:0 handler:^(UIAlertAction *acton){}];//创建按钮cancel以及对应事件
    
    //最后将这些按钮都添加到界面上去，显示界面
    [alertController addAction:fixAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController: alertController animated:YES completion:nil];

}
@end
